package com.j.lemon.learn.thread.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Condition
 * 配合重入锁使用，作用和synchronize中的wait()和notify()、notifyAll()一致
 */
public class ReentrantLockCondition implements Runnable {
    public static ReentrantLock reentrantLock = new ReentrantLock();
    public static Condition condition = reentrantLock.newCondition();
    @Override
    public void run() {
        try{
            reentrantLock.lock();
            condition.await();
            System.out.println("111");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            reentrantLock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ReentrantLockCondition r=new ReentrantLockCondition();
        Thread thread = new Thread(r);
        thread.start();
        System.out.println("222");
        Thread.sleep(2000);
        reentrantLock.lock();
        condition.signal();
        reentrantLock.unlock();
        System.out.println("333");
    }
}
